<template>
  <view class="form-overlay">
    <view class="form-container">
      <view class="form-header">
        <text>添加新项</text>
      </view>
      <view class="form-field">
        <text class="form-label">协议：</text>
        <picker
          mode="selector"
          :range="protocols"
          :value="protocolIndex"
          @change="onProtocolChange"
          :key="pickerKey"
          class="form-input"
        >
          {{ editItem.protocol || '请选择协议' }}
        </picker>
      </view>
      <view class="form-field">
        <text class="form-label">入口域名：</text>
        <input v-model="editItem.domain" class="form-input" />
      </view>
      <view class="form-field">
        <text class="form-label">二级域名：</text>
        <input v-model="editItem.sld" class="form-input" />
      </view>
      <view class="form-field">
        <text class="form-label">跳转地址：</text>
        <input v-model="editItem.target" class="form-input" />
      </view>
      <view class="form-field">
        <text class="form-label">平台：</text>
        <input v-model="editItem.platform" class="form-input" />
      </view>
      <view class="form-buttons">
        <button @click="saveAdd" class="form-button save-button">保存</button>
        <button @click="cancelAdd" class="form-button cancel-button">取消</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';

// 定义 props
const props = defineProps({
  protocols: {
    type: Array,
    required: true
  },
  editItem: {
    type: Object,
    required: true,
    default: () => ({
      protocol: '',
      domain: '',
      sld: '',
      target: '',
      platform: ''
    })
  }
});

// 定义 emits
const emit = defineEmits(['update:editItem', 'saveAdd', 'cancelAdd']);

// 响应式数据
const pickerKey = ref(0);

// 计算属性
const protocolIndex = computed(() => {
  return props.protocols.indexOf(props.editItem.protocol);
});

// 方法
const onProtocolChange = (e) => {
  console.log('Selected protocol index:', e.detail.value);
  const selectedProtocol = props.protocols[e.detail.value];
  console.log('Selected protocol:', selectedProtocol);

  // 使用 emit 更新父组件数据
  emit('update:editItem', {
    ...props.editItem,
    protocol: selectedProtocol
  });

  pickerKey.value++; // 更新 key 值，强制重新渲染
};

const saveAdd = () => {
  emit('saveAdd');
};

const cancelAdd = () => {
  emit('cancelAdd');
};
</script>

<style scoped>
/* 样式保持不变 */
.form-overlay {

}

.form-container {
  background-color: white;
  padding: 20rpx;
  border-radius: 10rpx;
  width: 80%;
  box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.1);
}

.form-header {
  font-size: 30rpx;
  font-weight: bold;
  margin-bottom: 20rpx;
  text-align: center;
}

.form-field {
  margin-bottom: 20rpx;
  display: flex;
  align-items: center;
}

.form-label {
  font-size: 24rpx;
  color: #666;
  margin-right: 10rpx;
  width: 100rpx;
  text-align: left;
}

.form-input {
  flex: 1;
  border: 1rpx solid #ccc;
  border-radius: 5rpx;
  padding: 10rpx;
  font-size: 24rpx;
}

.form-buttons {
  display: flex;
  justify-content: space-between;
  margin-top: 20rpx;
}

.form-button {
  background-color: #007aff;
  color: white;
  border: none;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
  font-size: 24rpx;
  width: 48%;
  text-align: center;
}

.save-button {
  background-color: #007aff;
}

.cancel-button {
  background-color: #ff5555;
}
</style>
